﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml;

namespace ReferenceSwap
{
    /// <summary>
    /// Content File - essentially anything not compiled
    /// </summary>
    public class ContentFile : FileReference
    {
        /// <summary>
        /// Create an instance of a Content File
        /// </summary>
        public ContentFile(FileReferenceType type) : base(type) { }

        /// <summary>
        /// Create an instance of a Content File from the include attribute
        /// </summary>
        /// <param name="type">file type</param>
        /// <param name="includeData">value of include attribute</param>
        public ContentFile(FileReferenceType type, string includeData)
            : base(type)
        {
            Guard.Against<ArgumentException>(string.IsNullOrEmpty(includeData), "IncludeData must have a valid value.");

            Path = includeData;
            Name = new FileInfo(includeData).Name;
        }
               
        /// <summary>
        /// Gets or Sets the setting for how to copy the file when a build occurs
        /// </summary>
        public FileCopySetting Copy { get; set; }

        /// <summary>
        /// Create a Visual studio project xml representation of the a content file item
        /// </summary>
        /// <returns>xml string</returns>
        public override string GenerateXml()
        {
            XmlDocument xml = new XmlDocument();
            XmlElement content = xml.CreateElement(Type.ToString());

            XmlAttribute include = xml.CreateAttribute("Include");
            include.Value = Path;
            content.Attributes.Append(include);

            XmlElement copy = xml.CreateElement("CopyToOutputDirectory");
            copy.InnerText = Copy.ToString();
            content.AppendChild(copy);

            if (IsLinked)
            {
                XmlElement link = xml.CreateElement("Link");
                link.InnerText = LinkPath;
                content.AppendChild(link);
            }

            return content.OuterXml;
        }
    }
}
